package hot100;

import java.util.ArrayList;

public class mergeKLists {
    public static class ListNode {
        int val;
        ListNode next = null;

        public ListNode(int val) {
            this.val = val;
        }
    }


    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param lists ListNode类ArrayList
     * @return ListNode类
     */
    public ListNode mergeKLists(ArrayList<ListNode> lists) {

        //0 / 1个链表的情况
        if (lists.size() == 0) {
            return null;
        }
        if (lists.size() == 1) {
            return lists.get(0);
        }

        ListNode ret = lists.get(0);

        for (int i = 1; i < lists.size(); i++) {
            //合并两个链表,将新合并和剩余的其中一个链表进合并
            ret = mergeTwo(ret, lists.get(i));

        }
        return ret;
    }

    public ListNode mergeTwo(ListNode list1, ListNode list2) {
        //cur头结点,指向val较小的节点
        ListNode cur = new ListNode(0);
        ListNode ret = new ListNode(0);
        if (list1 == null && list2 != null) {
            return list2;
        } else if (list1 != null && list2 == null) {
            return list1;
        } else if (list1 == null && list2 == null) {
            return null;
        }

        if (list1.val > list2.val) {
            cur.next = list2;
            ret = cur;
            cur = cur.next;
            list2 = list2.next;
            while (list1 != null && list2 != null) {
                if (list1.val < list2.val) {
                    cur.next = list1;
                    list1 = list1.next;
                    cur = cur.next;
                } else {
                    cur.next = list2;
                    list2 = list2.next;
                    cur = cur.next;
                }
            }
            if (list1 == null) {
                while (list2 != null) {
                    cur.next = list2;
                    list2 = list2.next;
                    cur = cur.next;
                }
            } else if (list2 == null) {
                while (list1 != null) {
                    cur.next = list1;
                    list1 = list1.next;
                    cur = cur.next;
                }
            }

        } else {
            cur.next = list1;
            ret = cur;
            cur = cur.next;
            list1 = list1.next;
            while (list1 != null && list2 != null) {
                if (list1.val < list2.val) {
                    cur.next = list1;
                    list1 = list1.next;
                    cur = cur.next;
                } else {
                    cur.next = list2;
                    list2 = list2.next;
                    cur = cur.next;
                }
            }
            if (list1 == null) {
                while (list2 != null) {
                    cur.next = list2;
                    list2 = list2.next;
                    cur = cur.next;
                }
            } else if (list2 == null) {
                while (list1 != null) {
                    cur.next = list1;
                    list1 = list1.next;
                    cur = cur.next;
                }
            }
        }
        //返回头结点
        return ret.next;
    }


}
